﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Web.Model
{
    public class MenuItem
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public string Title { get; set; }
        public int IsDefActive { get; set; }
        public string Url { get; set; }
        public MenuType MType { get; set; }
        public string Pid { get; set; }
        public int IsContainsDropDownMenu { get; set; }

        public MenuItem(string title, int isDefActive=0, string id="", string name="", string url = "", string pid="", 
            MenuType mType = MenuType.TopMenu, int isContainsDropDownMenu = 0)
        {
            this.Title = title;
            this.IsDefActive = isDefActive;
            this.Id = id;
            this.Name = name;
            this.Url = url;
            this.Pid = pid;
            this.MType = mType;
            this.IsContainsDropDownMenu = isContainsDropDownMenu;

        }
        public string LIClassName
        {
            get
            {
                if (MType == MenuType.DropdownMenu || IsContainsDropDownMenu == 1)
                {
                    return " class='dropdown' ";
                }
                else if (IsDefActive == 1)
                {
                    return " class='active' ";
                }
                return string.Empty;
            }

        }
        public string IClassName
        {
            get
            {
                if (MType == MenuType.TopMenu)
                {
                    return " class='fa' ";
                }
                return string.Empty;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public string ADataToggle
        {
            get
            {
                if (MType == MenuType.TopMenu && IsContainsDropDownMenu == 0)
                {
                    return " data-toggle='slidebar' ";
                }
                else if (MType == MenuType.DropdownMenu || IsContainsDropDownMenu == 1)
                {
                    return " data-toggle='dropdown' ";
                }
                return string.Empty;
            }
        }

        public string AClassName
        {
            get
            {
                if (MType == MenuType.DropdownMenu || IsContainsDropDownMenu == 1)
                {
                    return " class='dropdown-toggle' ";
                }
                return string.Empty;
            }
        }

        public string TitleNextHtml
        {
            get
            {
                if (MType == MenuType.DropdownMenu || IsContainsDropDownMenu == 1)
                {
                    return " <span class='caret'></span> ";
                }
                return string.Empty;
            }
        }

        /// <summary>
        /// A标签的下一个html字符串 传入 当前 顶部菜单ID，返回对应子菜单
        /// </summary>
        public string ANextHtml
        {
            get
            {   
                if (IsContainsDropDownMenu == 1)//当前顶部菜单 是否包含 dropDownMenu 类型的菜单
                {
                    return GetANextDivDropDownHtml(this.Id);
                }
                else //其他有 需要树状展示的子菜单项目
                {
                    return GetANextDivTreeHtml(this.Id);
                }
            }
        }


        /// <summary>
        /// 根据菜单父Id 组装下拉子菜单
        /// </summary>
        /// <param name="menuItemId"></param>
        /// <returns></returns>
        public string GetANextDivDropDownHtml(string menuItemId)
        {
            List<MenuItem> dropMenus = new List<MenuItem>()
            {
                new MenuItem(id:"1",title:"任务安排",pid:"1",mType: MenuType.DropdownMenu,url:"/office/taskplan.aspx"),
                new MenuItem(id:"2",title:"新闻公告",pid:"1",mType: MenuType.DropdownMenu,url:"/office/newsbulletin.aspx"),
                new MenuItem(id:"3",title:"工作日志",pid:"1",mType: MenuType.DropdownMenu,url:"/office/worklog.aspx"),
            };

            string resHtml = string.Empty;
            StringBuilder sbdropdownMenuHtml = new StringBuilder();
            sbdropdownMenuHtml.Append("<ul class='dropdown-menu' role='menu' style='min-width: 115px;'>");
            for (int j = 0; j < dropMenus.Count; j++)
            {
                var menu = dropMenus[j];
                sbdropdownMenuHtml.Append("<li><a href=\"" + menu.Url + "\" data-toggle=\"navtab\" data-options=\"{id:'" + menu.Id + "', title:'" + menu.Title + "'}\">" + menu.Title + "</a></li>");
                if (j < dropMenus.Count-1)
                {
                    sbdropdownMenuHtml.Append("<li class='divider'></li>");
                }
            }
            sbdropdownMenuHtml.Append("</ul>");
            resHtml = sbdropdownMenuHtml.ToString();
            return resHtml;
        }

        
        /// <summary>
        /// 根据菜单父Id 组装树状子菜单
        /// </summary>
        /// <param name="menuItemId"></param>
        /// <returns></returns>
        public string GetANextDivTreeHtml(string menuItemId)
        {
            string resHtml = string.Empty;
            StringBuilder sbTreeHtml = new StringBuilder();
            sbTreeHtml.Append("<div class='items hide' data-noinit='true'>");
            if (menuItemId == "0")
            {
                List<TreeItem> trees = GetTreeMenu1();
                foreach (var tree in trees)
                {
                    sbTreeHtml.AppendFormat(@"<ul id='{0}' class='ztree ztree_main' data-toggle='ztree' 
                    data-on-click='MainMenuClick' data-expand-all='true' {1} {2}>", tree.Id, tree.Data_Faicon, tree.Data_Tit);

                    #region 大客户
                    var childs = tree.Children.FirstOrDefault();
                    sbTreeHtml.AppendFormat(@"<li data-id='{0}' data-pid='{1}' {2} {3}>{4}</li>"
                        , childs.Id, "0", childs.Data_Faicon, childs.Data_Faicon_Close, childs.Title);

                    var childss = childs.Children;

                    foreach (var child in childss)
                    {
                        sbTreeHtml.AppendFormat(@"<li data-id='{0}' data-pid='{1}' data-url='{2}' {3} {4}>{5}</li>"
                            , child.Id, child.Pid, child.Data_Url, child.Data_TabId, child.Data_Faicon, child.Title);
                    } 
                    #endregion

                    #region 所有客户
                    childs = tree.Children.ToList()[1];
                    sbTreeHtml.AppendFormat(@"<li data-id='{0}' data-pid='{1}' {2} {3}>{4}</li>"
                        , childs.Id, "0", childs.Data_Faicon, childs.Data_Faicon_Close, childs.Title);

                    childss = childs.Children;

                    foreach (var child in childss)
                    {
                        sbTreeHtml.AppendFormat(@"<li data-id='{0}' data-pid='{1}' data-url='{2}' {3} {4}>{5}</li>"
                            , child.Id, child.Pid, child.Data_Url, child.Data_TabId, child.Data_Faicon, child.Title);
                    } 
                    #endregion


                    sbTreeHtml.Append("</ul>");
                }

                trees = GetTreeMenu2();
                foreach (var tree in trees)
                {
                    sbTreeHtml.AppendFormat(@"<ul id='{0}' class='ztree ztree_main' data-toggle='ztree' 
                    data-on-click='MainMenuClick' data-expand-all='true' {1} {2}>", tree.Id, tree.Data_Faicon, tree.Data_Tit);

                    var childs = tree.Children.FirstOrDefault();
                    sbTreeHtml.AppendFormat(@"<li data-id='{0}' data-pid='{1}' {2} {3}>{4}</li>"
                        , childs.Id, "0", childs.Data_Faicon, childs.Data_Faicon_Close, childs.Title);

                    var childss = childs.Children;

                    foreach (var child in childss)
                    {
                        sbTreeHtml.AppendFormat(@"<li data-id='{0}' data-pid='{1}' data-url='{2}' {3} {4}>{5}</li>"
                            , child.Id, child.Pid, child.Data_Url, child.Data_TabId, child.Data_Faicon, child.Title);
                    }
                    sbTreeHtml.Append("</ul>");
                }
            }
            else if (menuItemId == "3")
            {
                var  trees = GetTreeMenu3();
                foreach (var tree in trees)
                {
                    sbTreeHtml.AppendFormat(@"<ul id='{0}' class='ztree ztree_main' data-toggle='ztree' 
                    data-on-click='MainMenuClick' data-expand-all='true' {1} {2}>", tree.Id, tree.Data_Faicon, tree.Data_Tit);

                    var childs = tree.Children.FirstOrDefault();
                    sbTreeHtml.AppendFormat(@"<li data-id='{0}' data-pid='{1}' {2} {3}>{4}</li>"
                        , childs.Id, "0", childs.Data_Faicon, childs.Data_Faicon_Close, childs.Title);

                    var childss = childs.Children;

                    foreach (var child in childss)
                    {
                        sbTreeHtml.AppendFormat(@"<li data-id='{0}' data-pid='{1}' data-url='{2}' {3} {4}>{5}</li>"
                            , child.Id, child.Pid, child.Data_Url, child.Data_TabId, child.Data_Faicon, child.Title);
                    }
                    sbTreeHtml.Append("</ul>");
                }
            }
            sbTreeHtml.Append("</div>");
            resHtml = sbTreeHtml.ToString();
            return resHtml;
        }
        
        //public double SortPoints { get; set; }

        //public string AddedUserId { get; set; }

        //public string Added { get; set; }

        //public string Modify

        #region 客户管理菜单
        public List<TreeItem> GetTreeMenu1()
        {
            List<TreeItem> childss = new List<TreeItem>(){               
                 new TreeItem("102","101","潜在客户",3,"/kehu/qianzaikehu.aspx"),
                  new TreeItem("103","101","正式客户",3,"/kehu/zhengshikehu.aspx"),
                  new TreeItem("104","101","流失客户",3,"/kehu/liushikehu.aspx"),
                  new TreeItem("105","101","报备客户",3,"/kehu/baobeikehu.aspx")
            };

            List<TreeItem> childss1 = new List<TreeItem>(){               
                 new TreeItem("111","110","大客户基本信息",3,"/kehu/dakehu.aspx"),
                  new TreeItem("112","110","大客户委托记录",3,"/kehu/dakehu.aspx"),
                  new TreeItem("113","110","外网委托复核",3,"/kehu/dakehu.aspx"),
                  new TreeItem("114","110","咨询建议处理",3,"/kehu/dakehu.aspx"),
                  new TreeItem("115","110","客户委托排名",3,"/kehu/dakehu.aspx")
            };

            List<TreeItem> childs = new List<TreeItem>()
            {
                new TreeItem("110","100","大客户",2,url:"",children:childss1),
                new TreeItem("101","100","所有客户",2,url:"",children:childss)
            };


            List<TreeItem> trees = new List<TreeItem>();
            trees.Add(new TreeItem("100", "1", "客户管理", 1, "", childs));

            return trees;
        }
        public List<TreeItem> GetTreeMenu2()
        {
            List<TreeItem> childss = new List<TreeItem>(){               
                 new TreeItem("202","201","信用等级",3,"/kehu/xinyongdengji.aspx"),
                  new TreeItem("203","201","信用等级变更",3,"/kehu/xinyongdengjibiangeng.aspx"),
                  new TreeItem("204","201","往来单位等级变更单",3,"/kehu/wanglaidanweidengji.aspx"),
                   new TreeItem("205","201","往来单位分类",3,"/kehu/wanglaidanweifenlei.aspx")
            };

            List<TreeItem> childs = new List<TreeItem>()
            {
                new TreeItem("201","200","其他项目",2,url:"",children:childss)
               
            };


            List<TreeItem> trees = new List<TreeItem>();
            trees.Add(new TreeItem("200", "2", "其他", 1, "", childs));
            return trees;
        } 

        #endregion


        public List<TreeItem> GetTreeMenu3()
        {
            List<TreeItem> childss = new List<TreeItem>(){               
                 new TreeItem("102","101","联系人信息",3,"/lianxiren/lianxiren.aspx"),
                  new TreeItem("103","101","联系记录",3,"/lianxiren/lianxiren.aspx")
            };

            List<TreeItem> childs = new List<TreeItem>()
            {
                new TreeItem("101","100","联系管理",2,url:"",children:childss)
            };


            List<TreeItem> trees = new List<TreeItem>();
            trees.Add(new TreeItem("100", "1", "联系人管理", 1, "", childs));

            return trees;
        }

       
    }
}